package com.kuyu.wx;

import com.kuyu.model.WxAccessToken;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletResponse;

@Controller
@Slf4j
public class WxController {
    @GetMapping("/auth")
    @ResponseBody
    public void auth(@RequestParam("code") String code) {
        log.info("进入auth方法。。。");
        log.info("code={}", code);

        String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=wxa333c9dbd51a3b3a&secret=bcdf0560126886f1b0fa1cab0476cb63&code=" + code + "&grant_type=authorization_code";
        RestTemplate restTemplate = new RestTemplate();
        String response = restTemplate.getForObject(url, String.class);
        log.info("response={}", response);
    }
    //获取accessToken
    @RequestMapping("/at")
    public void getAccessToken(HttpServletResponse response) throws Exception{
        //向浏览器输出内容
        response.getWriter().println(WxAccessToken.getAccessToken());
    }

}
